<?php
/**
 * Created by PhpStorm.
 * User: ytlmi
 * Date: 2018/3/8
 * Time: 16:37
 */

namespace org\util\forensictool;


class LittleTools
{
    protected static $instance;//object 对象实例
    /**
     * @return LittleTools
     * 实例化当前类
     */
    static public function instance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new static();
        }
        return self::$instance;
    }

    //汉字年份
    protected function zhYear($data){
        $data=intval($data);
        $cn0=array('〇','一','二','三','四','五','六','七','八','九');
        if(is_int($data)){
            for($i=0,$str='';$i<strlen($data);$i++){
                $str_cn=$cn0[substr($data,$i,1)];
                $str=$str.$str_cn;
            }
        }
        return $str;
    }

    /**
     * @param int $num
     * @return string
     * 数字转换成汉字,暂时支持到千万
     */
    public function numToZh($num){
        if ($num===0) return '零';
        $trans = function ($data){
            $data=intval($data);
            $cn0=array('零','一','二','三','四','五','六','七','八','九');
            $cn1=array('','十','百','千');
            $numCN=null;
            $len=strlen($data);
            if($len!=0){
                if($len==2 && substr($data,0,1)==1){
                    $num=substr($data,1,1);
                    $numCN='十'.$cn0[$num];
                }else{
                    for($i=0;$i<$len;$i++){
                        $num=substr($data,$i,1);
                        if($num==0){
                            $cn2='零';
                        }else{
                            $cn2=$cn0[$num].$cn1[$len-$i-1];
                        }
                        $numCN=$numCN.$cn2;
                    }
                }
                for($j=1;$j<$len+1;$j++){
                    $k=substr($data,$len-$j,1);
                    if($k=='0'){
                        $numCN=mb_substr($numCN,0,-1);
                    }else{
                        break;
                    }
                }
            }
            return $numCN;
        };
        $num=intval($num);
        $numCN=null;
        $len=strlen($num);
        if($num==10){
            $numCN='十';
        }else{
            if($len<9){
                if($len>4){
                    $num0=substr($num,0,$len-4);
                    $num1=substr($num,$len-4);
                    $num2=substr($num,$len-5,1);
                    $num3=substr($num,$len-4,1);
                    $numCN0=$trans($num0);
                    $numCN1=$trans($num1);
                    if($num2==0 || $num3==0){
                        $str0='万零';
                    }else{
                        $str0='万';
                    }
                    $numCN=$numCN0.$str0.$numCN1;
                }else{
                    $numCN=$trans($num);
                }
            }else{
                error_reporting('too big!!!!');
            }
            $numCN=str_replace('零零','零',$numCN);
        }
        return $numCN;
    }

    /**
     * @param mixed $date
     * @param string $numType
     * @return string
     * @throws \Exception
     * 日期转换汉字,支持20171212、2017/12/12、2017-12-12格式
     * $numType 可选'zh'(生成"二〇一七年十二月十二日")/'en'(生成"2017年12月12日")
     */
    public function dateToZh($date,$numType='zh')
    {
        $str_date=strval($date);
        if(is_numeric($date) && strlen($str_date)==8){
            $str[0]=substr($str_date,0,4);
            $str[1]=substr($str_date,4,2);
            $str[2]=substr($str_date,6,2);
        }elseif(preg_match('/\//',$str_date)){
            $str=preg_split('/\//',$str_date);
        }elseif(preg_match('/-/',$str_date)){
            $str=preg_split('/-/',$str_date);
        }else{
            throw new \Exception('日期格式错误：'.var_dump($date));
        }
        switch ($numType){
            case 'zh':
                return $this->zhYear($str[0]).'年'.$this->numToZh($str[1]).'月'.$this->numToZh($str[2]).'日';
            case 'en':
                return $str[0].'年'.$str[1].'月'.$str[2].'日';
            default:
                throw new \Exception('数字格式错误：'.var_dump($numType));
        }
    }


    /**
     * @param string $num
     * @return array [0]=>底数,[1]=>幂
     * @throws \Exception
     * 把数字改写成123x10的X次方(上标)形式
     */
    public static function ciFang($num){
        if(!is_numeric($num)) throw new \Exception('数字格式错误');
        $num=strval($num);
        $num=str_replace('e','E',$num);
        if(stripos($num,'E')){
            $a=strpos($num,'E');
            $ciFang=substr($num,$a+1);
            $CPI0=substr($num,0,7);
            $b=substr($CPI0,6,1);
            $CPI_Fore=substr($CPI0,0,6);
            if($b>4){
                $CPI_Fore=$CPI_Fore+0.0001;
            }
        }else{
            if($num>1){
                $CPI= sprintf("%.0f",$num);//四舍五入取整数
                $countCPI= strlen($CPI);//取长度
                if($countCPI>5){
                    $CPI0=substr($num,0,6);
                }else{
                    $a=substr($num,0,$countCPI-1);
                    $b=substr($num,$countCPI+1,7-$countCPI);
                    $CPI0=$a.$b;
                }
                $c=substr($CPI0,5,1);
                $CPI1=substr($CPI0,0,5);
                if($c>4){
                    $CPI1++;
                }
                $CPI_Fore0=substr($CPI1,0,1);
                $CPI_Fore1=substr($CPI1,1);
                $CPI_Fore=$CPI_Fore0.'.'.$CPI_Fore1;
                $ciFang =  $countCPI-1;

            }else{
                for($i=0;$i<40;$i++){
                    $j = substr($num,$i,1);
                    if($j!=0){
                        break;
                    }
                }
                $CPI0=substr($num,$i,6);
                $c=substr($CPI0,5,1);
                $CPI1=substr($CPI0,0,5);
                if($c>4){
                    $CPI1++;
                }
                $CPI_Fore0=substr($CPI1,0,1);
                $CPI_Fore1=substr($CPI1,1);
                $CPI_Fore=$CPI_Fore0.'.'.$CPI_Fore1;
                $ciFang = 1-$i;
            }
        }
        return array($CPI_Fore,$ciFang);
    }

    public static function formatSex($sex)
    {
        $map=['男','女','未提供'];
        return is_numeric($sex) ? $map[$sex] : $sex;
    }

    public static function makeZhCalling($call)
    {
        $map=['F'=>'父亲','M'=>'母亲','C'=>'孩子'];
        foreach ($map as $key=>$zhCalling) {
            if (preg_match('/'.$key.'/',$call)){
                return $zhCalling;
            }
        }
        return false;
    }

}